<?php
namespace app\admin\controller;
use think\Db;

/**
 * 数据导出
 * Class Export
 * @package app\admin\controller
 */
class Export
{
	//文件导出 https://blog.csdn.net/yhlas/article/details/80650363
	public function export(){
		$xlsName  = "干部&家属信息表";
		$xlsCell  = array(
				array('g_name','干部姓名'),
				array('g_sfid','干部证件号码'),
				array('relationship','亲属关系'),
				array('q_name','亲属姓名'),
				array('q_sfid','证件号码'),
				array('cat_name','项目名称'),
				array('amount','金额'),
				array('release_time','实施时间'),
				array('release_address','实施地点'),
		);
		$xlsData ='';
		$xlsData = Db::table('hb_income')
				->where('id',1)
				->select();

		$this->exportExcel($xlsName,$xlsCell,$xlsData);;
	}

	public function exportExcel($expTitle,$expCellName,$expTableData){
		$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称

		$fileName =$xlsTitle . date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
		$cellNum = count($expCellName);
		$dataNum = count($expTableData);

		// 引入核心文件
		require'../extend/PHPExcel/PHPExcel/PHPExcel.php';
		$objPHPExcel = new \PHPExcel();
		$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

		$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
		// $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
		for($i=0;$i<$cellNum;$i++){
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
		}
		// Miscellaneous glyphs, UTF-8
		for($i=0;$i<$dataNum;$i++){
			for($j=0;$j<$cellNum;$j++){
				$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
			}
		}
		$styleThinBlackBorderOutline = array(
				'borders' => array(
						'allborders' => array(
							//设置全部边框
								'style'=>\PHPExcel_Style_Border::BORDER_THIN//粗的是thick
						),

				),
		);
		$objPHPExcel->getActiveSheet()->getStyle( 'A1:K12')->applyFromArray($styleThinBlackBorderOutline);
		header('pragma:public');
		header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
		header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;
	}

}
